/*
 * generate by table-builder.Don't modify it
 */
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using System.Collections.Generic;


namespace CfgTable
{
    public partial class TbCfgI18N
    {
        private readonly Dictionary<int, CfgI18N> _dataMap;
        private readonly List<CfgI18N> _dataList;


        public TbCfgI18N(ByteBuf _buf)
        {
            //first read common data
            _TbCommonI18N _commonData = null;
            var commonSize = _buf.ReadSize();
            if (commonSize > 0)
            {
                _commonData = new _TbCommonI18N(_buf);
            }

            //then read row data
            var size = _buf.ReadSize();
            _dataMap = new Dictionary<int, CfgI18N>(size * 3 / 2);
            _dataList = new List<CfgI18N>(size);

            for (int i = 0; i < size; i++)
            {
                CfgI18N _v;
                _v = CfgI18N.DeserializeCfgI18N(_buf, _commonData);
                _dataList.Add(_v);
                _dataMap.Add(_v.Id, _v);
            }

            // read extra fields 

            PostInit();
        }

        public IReadOnlyDictionary<int, CfgI18N> DataMap => _dataMap;
        public IReadOnlyList<CfgI18N> DataList => _dataList;

        public CfgI18N Get(int key)
        {
            if (_dataMap.TryGetValue(key, out var v))
            {
                return v;
            }
            IQIGame.Onigao.Framework.LogGame.LogError("[TbCfgI18N] config not found,id:" + key.ToString());

            return null;
        }

        public CfgI18N GetWithoutError(int key) => _dataMap.TryGetValue(key, out var v) ? v : null;

        /// <summary>
        /// table data file
        /// </summary>
        public static string TableFileName { get; } = "i18n";

        /// <summary>
        /// load table function
        /// </summary>
        public static TbCfgI18N LoadFromFunc(System.Func<string, ByteBuf> loadFunc)
        {
            return new TbCfgI18N(loadFunc(TableFileName));
        }

        /// <summary>
        /// post process table
        /// </summary>
        partial void PostInit();

    }

    public partial class CfgI18N
    {
        private CfgI18N(ByteBuf _buf, _TbCommonI18N _commonData)
        {
            Id = _buf.ReadInt();
            { int dataIndex = _buf.ReadInt() - 1; Str = _commonData._field0[dataIndex]; }
            RefId = _buf.ReadInt();

            PostInit();
        }

        internal static CfgI18N DeserializeCfgI18N(ByteBuf _buf, _TbCommonI18N _commonData)
        {
            return new CfgI18N(_buf, _commonData);
        }

        /// <summary>
        /// major key
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// 文字
        /// </summary>
        public string Str { get; private set; }
        /// <summary>
        /// 引用id
        /// </summary>
        public int RefId { get; private set; }


        /// <summary>
        /// post process table
        /// </summary>
        partial void PostInit();
    }

    /// <summary>
    /// internal common data.Optimize memory
    /// </summary>
    internal class _TbCommonI18N
    {

        internal IReadOnlyList<string> _field0 { get; private set; }
        internal _TbCommonI18N(ByteBuf _buf)
        {
            { int __n0 = _buf.ReadSize(); var __f0 = new string[__n0]; _field0 = __f0; for (var __i0 = 0 ; __i0 < __n0 ; __i0++ ){ string __v0; __v0 = _buf.ReadString(); __f0[__i0] = __v0;} }
        }

    }
}
